package br.jcrsistemas.syscomercio.janelas;

import java.awt.Dialog.ModalExclusionType;
import java.awt.Dialog.ModalityType;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;

import org.hibernate.HibernateException;

import br.jcrsistemas.componentes.SysFrame;
import br.jcrsistemas.componentes.SysTextField;
import br.jcrsistemas.syscomercio.bean.Comanda;
import br.jcrsistemas.syscomercio.dao.ComandaDAO;
import br.jcrsistemas.util.Configuracoes;
import br.jcrsistemas.util.Util;

public class FrmGerenciaComanda extends JDialog {

	private JPanel contentPane;
	private Comanda comanda;
	private final JLabel lblTitulo = new JLabel("Comanda:");
	private final JSeparator separator = new JSeparator();
	private final JLabel lblCliente = new JLabel("Cliente:");
	private final SysTextField txtCliente = new SysTextField();
	private final JButton btnOk = new JButton("Ok");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrmGerenciaComanda frame = new FrmGerenciaComanda(new Comanda(1), false, null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrmGerenciaComanda(Comanda comanda, boolean modal, JFrame owner) {
		super(owner, modal);
		setLocationRelativeTo(null);
		this.comanda = comanda;
		initialize();
	}
	private void initialize() {
		inicia();
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 1010, 720);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		lblTitulo.setIcon(new ImageIcon(FrmGerenciaComanda.class.getResource("/br/jcrsistemas/icones/table-64.png")));
		lblTitulo.setFont(new Font("Segoe Print", Font.BOLD, 20));
		lblTitulo.setBounds(6, 11, 571, 71);
		
		contentPane.add(lblTitulo);
		separator.setBounds(0, 94, 994, 2);
		
		contentPane.add(separator);
		lblCliente.setBounds(6, 107, 99, 14);
		
		contentPane.add(lblCliente);
		txtCliente.setBounds(99, 105, 515, 20);
		
		contentPane.add(txtCliente);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				comanda.setCliente(txtCliente.getText());
				ComandaDAO dao = new ComandaDAO();
				try {
					dao.gravar(comanda);
				}
				catch (HibernateException e1) {
					Util.trataErro(e1);
				}
				dispose();
				
			}
		});
		btnOk.setBounds(30, 339, 89, 23);
		
		contentPane.add(btnOk);
		
	}
	
	public void inicia() {
		txtCliente.setTamanho(60);
		setTitle("Comanda: " + comanda.getNumero());
		txtCliente.setText(comanda.getCliente());
		lblTitulo.setText("Comanda: " + comanda.getNumero());
	}
	
//	@Override
//	public void doAoFechar() {
//		if(comanda.isAberta()) {
//			if (JOptionPane.showConfirmDialog(this, "Gostaria de fechar a comanda?") == JOptionPane.YES_OPTION ) {
//				comanda.setAberta(false);
//			}
//		}
//		super.doAoFechar();
//	}
}
